package com.jic.product.controller.rest;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.entity.Category;
import com.jic.product.entity.ProductBrand;
import com.jic.product.manage.request.CategoryMerchantRelationRequest;
import com.jic.product.manage.request.CategoryRequest;
import com.jic.product.manage.response.CategorySecondRest;
import com.jic.product.manage.response.CategorySecondSonRest;
import com.jic.product.rest.request.ProductBrandRestRequest;
import com.jic.product.rest.request.ProductCategoryRestRequest;
import com.jic.product.rest.response.ProductBrandRestResponse;
import com.jic.product.rest.response.ProductCategoryRestResponse;
import com.jic.product.service.CategoryMerchantRelationService;
import com.jic.product.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by ZXK on 2020/1/9 11:04
 * 前台类目查询
 */
@RestController
@RequestMapping("/categoryRest")
@Api(value = "API-类目查询", tags = "API-类目查询")
@Slf4j
public class ApiCategoryRest {

    @Autowired
    private CategoryService categoryService;


    @Autowired
    private CategoryMerchantRelationService categoryMerchantRelationService;


    /*
     *查询当前生效的前台类目的所有二级类目
     * type=2  前台类目
     * level=2  前台二级类目
     * */
    @ResponseBody
    @PostMapping(value = "/queryBeforeSecondCategory")
    @ApiOperation(value = "查询所有二级类目", notes = "查询所有二级类目")
    public RestResult queryBeforeSecondCategory(Long id) {
        CategoryRequest categoryRequest = new CategoryRequest();
        categoryRequest.setType(2);
        categoryRequest.setLevel(2);
        categoryRequest.setParentId(String.valueOf(id));
        RestResult restResult = new RestResult();
        try {
            List<CategorySecondRest> categoryResponses = categoryService.queryBeforeSecondCategory(categoryRequest);
            restResult.setData(categoryResponses);
            restResult.setCode("0000");
            restResult.setMsg("调用成功");
        } catch (Exception e) {
            log.error("9999", e.getLocalizedMessage());
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }

        return restResult;
    }

    /*
     *查询当前二级类目的所有子类目
     * type=2  前台类目
     * level=2  前台二级类目
     * */
    @ResponseBody
    @PostMapping(value = "/queryBeforeSecondSonCategory")
    @ApiOperation(value = "查询当前二级类目子类目", notes = "查询当前二级类目子类目")
    public RestResult queryBeforeSecondSonCategory(Long id) {
        log.info("jic-product===============》查询当前二级类目的所有子类目,{}", id);
        CategoryRequest categoryRequest = new CategoryRequest();
        categoryRequest.setType(2);
        categoryRequest.setParentId(String.valueOf(id));
        RestResult restResult = new RestResult();
        try {
            List<CategorySecondSonRest> categoryResponses = categoryService.queryBeforeSecondSonCategory(categoryRequest);
            restResult.setData(categoryResponses);
            restResult.setCode("0000");
            restResult.setMsg("调用成功");
        } catch (Exception e) {
            log.error("9999", e.getLocalizedMessage());
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }

        return restResult;
    }


    /*
       查询首页类目 默认展示四个
     * */
    @ResponseBody
    @PostMapping(value = "/queryRestCategoryForIndex")
    @ApiOperation(value = "查询首页类目 默认展示四个", notes = "查询首页类目 默认展示四个")
    public RestResult queryRestCategoryForIndex() {
        log.info("jic-product===============》查询首页类目,{}");
        return categoryService.queryRestCategoryForIndex();
    }

    /*
     *Pc返回所有的二级类目及其子类目
     * type=2  前台类目
     * */
    @ResponseBody
    @PostMapping(value = "/queryBeforeSecondSonCategoryForPc")
    @ApiOperation(value = "Pc返回所有的二级类目及其子类目")
    public RestResult queryBeforeSecondSonCategoryForPc(@RequestBody CategoryRequest categoryRequest) {
        log.info("jic-product===============》Pc返回所有的二级类目及其子类目,{}");
        if (StringUtils.isNotEmpty(categoryRequest.getId())) {
            //前台类目暂时写死
            categoryRequest.setParentId(categoryRequest.getId());
        } else {
            //前台类目暂时写死
            categoryRequest.setParentId("11");
        }
        try {
            List<CategorySecondSonRest> categoryResponses = categoryService.queryBeforeSecondSonCategoryForPc(categoryRequest);
            return RestResult.success(categoryResponses);
        } catch (Exception e) {
            log.error("9999", e.getLocalizedMessage());
            return RestResult.error("9999", e.getLocalizedMessage());
        }
    }

    /*
     *查询当前类目所有叶子节点
     * */
    @ResponseBody
    @PostMapping(value = "/queryLastCategory")
    @ApiOperation(value = "查询当前类目所有叶子节点")
    public RestResult queryLastCategory(@RequestBody CategoryRequest categoryRequest) {
        log.info("jic-product===============》查询当前二级类目的所有叶子节点,{}", categoryRequest);
        try {
            List<CategorySecondRest> categoryResponses = categoryService.queryLastCategory(Long.valueOf(categoryRequest.getId()));
            return RestResult.success(categoryResponses);
        } catch (Exception e) {
            log.error("9999", e.getLocalizedMessage());
            return RestResult.error("9999", e.getLocalizedMessage());
        }
    }


    /*
     *查询二级店铺分类
     * */
    @ResponseBody
    @PostMapping(value = "/queryStoreSecondCategory")
    @ApiOperation(value = "查询店铺二级类目")
    public RestResult queryStoreSecondCategory(Long deptId) {
        return categoryMerchantRelationService.queryStoreSecondCategory(deptId);
    }

    /*
     *
     * */
    @ResponseBody
    @PostMapping(value = "/queryStoreSecondSonCategory")
    @ApiOperation(value = "根据店铺二级分类查询下级类目")
    public RestResult queryStoreSecondSonCategory(@RequestBody CategoryMerchantRelationRequest categoryMerchantRelationRequest) {
        return categoryMerchantRelationService.queryStoreSecondSonCategory(categoryMerchantRelationRequest);
    }


    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取前台所有叶子类目")
    @ResponseBody
    @PostMapping(value = "/listRestPage")
    public RestResult<PageResult<ProductCategoryRestResponse>> listRestPage(@RequestBody ProductCategoryRestRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        Category category = BeanUtil.copy(form, Category.class);
        PageResult<Category> result = categoryService.listRestPage(category, page);
        List<ProductCategoryRestResponse> productCategoryRestResponseList = result.getRows().stream().map(temp -> {
            ProductCategoryRestResponse productCategoryRestResponse = BeanUtil.copy(temp, ProductCategoryRestResponse.class);
            return productCategoryRestResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<ProductCategoryRestResponse> pageResult = new PageResult<>();
        pageResult.setRows(productCategoryRestResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);

    }
}
